(function($) {
    $.fn.xpfacePost = function(options) {
        //default settings
        var settings = {
            succAction:"msg"   //msg,redirect
            ,message:"succ"
            ,url:""
        };
        
        //marge settings
        if(options) {
			$.extend(settings, options);
		}
        
        var formJQ = this;
        
        var callbackFun = function(data) {
            var _data = eval("("+data+");");
            if (_data["status"] == "failing") {
                for (var i=0; i<_data["data"].length; i++) {
                    //alert($("[name="+_data["data"][i].key+"]").parent().parent().find("span.errmsg").html());
                    $("[name="+_data["data"][i].key+"]").parent().parent().find("span.errmsg").html(_data["data"][i].value);
                }
                return ;
            }
            if (_data["status"] == "succ") {
                if (settings.succAction == "redirect") {
                    location.href = settings.url;
                    return ;
                }
                if (settings.succAction == "msg") {
                    alert(settings.message);
                    return ;
                }
            }
        }
        
        var postForm = function() {
            //alert(formJQ.attr("action"));
            if (formJQ.attr("action") == undefined) {
                alert("Error.1002: form id is not exists.");
                return ;
            }
            
            formJQ.find("input:submit").each(function(){
                $(this).attr("disabled", true);
            });
            
            var postData = {};
            
            formJQ.find("span.compoment input:text").each(function(){
                //alert($(this).val());
                postData[$(this).attr("name")] = $(this).val();
            });
            formJQ.find("span.compoment input:hidden").each(function(){
                //alert($(this).val());
                postData[$(this).attr("name")] = $(this).val();
            });
            formJQ.find("span.compoment input:password").each(function(){
                //alert($(this).val());
                postData[$(this).attr("name")] = $(this).val();
            });
            formJQ.find("span.compoment textarea").each(function(){
                //alert($(this).val());
                postData[$(this).attr("name")] = $(this).val();
            });
            formJQ.find("span.compoment input:radio").each(function(){
                if ($(this).attr("checked") && $(this).val() != undefined) {
                    //alert($(this).val());
                    if (postData[$(this).attr("name")] == undefined) {
                        postData[$(this).attr("name")] = [];
                    }
                    postData[$(this).attr("name")] = $(this).val();
                }
            });
            
            formJQ.find("span.compoment input:checkbox").each(function(){
                if ($(this).attr("checked")) {
                    if (postData[$(this).attr("name")] == undefined) {
                        postData[$(this).attr("name")] = [];
                    }
                    var i = postData[$(this).attr("name")].length;
                    //alert($(this).attr("name"));
                    postData[$(this).attr("name")][i] = $(this).val();
                }
            });
            
            
            formJQ.find("span.compoment select").each(function(){
                //alert($(this).val());
                postData[$(this).attr("name")] = $(this).val();
            });
            
            $.post(formJQ.attr("action"), postData, callbackFun);
        }
        
        //alert(postData.address_name);
        this.submit(function() {
            //alert(postData.address_name);
            postForm();
            return false;
        });
        
    }
    
})(jQuery);